#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>

extern void _start(void);
extern void etext(void);
extern void * __libc_stack_end();

/* Obtain a backtrace and print it to stdout */
void print_trace(void)
{
    void *array[10];
    size_t size;
    char **strings;
    size_t i;

    size = backtrace(array, 10);
    for (i = 0; i < size; i ++)
        printf("%p\n", array[i]);

    /* Use -dynamic options to get the symbols */
    strings = backtrace_symbols(array, size);

    printf("Obtained %zd stack frames.\n", size);

    for (i = 0; i < size; i ++)
        printf("%s\n", strings[i]);

    printf("in print_trace\n");
    printf("_start is %p\n", _start);
    printf("etext is %p\n", etext);
    printf("__libc_stack_end is %p\n", __libc_stack_end);

    free(strings);
}

/* A dummy function to make the backtrace more interesting */
void dummy_function(void)
{
    printf("in dummy_function\n");
    printf("_start is %p\n", _start);
    printf("etext is %p\n", etext);
    printf("__libc_stack_end is %p\n", __libc_stack_end);

    print_trace();
}

int main(void)
{
    printf("in main\n");
    printf("_start is %p\n", _start);
    printf("etext is %p\n", etext);
    printf("__libc_stack_end is %p\n", __libc_stack_end);

    dummy_function();
    return 0;
}













